const path = require('path');
const fs = require('fs');
const projFrameJson = require('../config/fileConf')

const renderDirOrFile = (root, curItem) => {
    const { type, name, children = [] } = curItem;
    const curDir = path.join(root, name);
    if(type === 'dir') fs.mkdirSync(path.join(root, name));
    if(type === 'file') fs.writeFileSync(path.join(root, name), '');
    if(children.length) {
        for (const child of children) {
            renderDirOrFile(curDir, child);
        }
    }
}

const entry = (projConf) => {
    const root = path.join(projConf.name);
    if(!fs.existsSync(root)) fs.mkdirSync(projConf.name)
    else return new Error(`dir ${projConf.name} already exists, please remove first .`)
    for (const item of projFrameJson) {
        renderDirOrFile(root, item);
    }
}

module.exports = entry;
